<?php

declare(strict_types=1);

namespace DreamCat\StateMachineTest\Cases\Config;

use DreamCat\StateMachine\Error\DefineConfigException;
use DreamCat\StateMachine\Parser\Inner\ParseConfig;
use PHPUnit\Framework\TestCase;

/**
 * 通用配置解析器测试用例
 * @author vijay
 */
class CommonParseConfigTest extends TestCase
{
    /**
     * 验证json不存在的情况
     * @return void
     * @throws DefineConfigException
     */
    public function testNoJson(): void
    {
        $this->expectException(DefineConfigException::class);
        $use = uniqid("testCase");
        $file = uniqid("noFile") . ".json";
        $this->expectErrorMessage("找不到 {$use} [{$file}]");
        $o = new ParseConfig();
        $o->getJsonFile($file, $use);
    }

    /**
     * 验证json文件为空的情况
     * @return void
     * @throws DefineConfigException
     */
    public function testEmptyJson(): void
    {
        $this->expectException(DefineConfigException::class);
        $use = uniqid("testCase");
        $file = __DIR__ . "/../../resources/error/empty.txt";
        $this->expectErrorMessage("{$use} 内容为空 [{$file}]");
        $o = new ParseConfig();
        $o->getJsonFile($file, $use);
    }

    /**
     * 验证json文件不合法的情况
     * @return void
     * @throws DefineConfigException
     */
    public function testErrorJson(): void
    {
        $this->expectException(DefineConfigException::class);
        $use = uniqid("testCase");
        $file = __DIR__ . "/../../resources/error/error.txt";
        $this->expectErrorMessage("{$use} [{$file}] json解析失败 <4> [Syntax error]");
        $o = new ParseConfig();
        $o->getJsonFile($file, $use);
    }

    /**
     * 测试json不符合schema的情况
     * @return void
     * @throws DefineConfigException
     */
    public function testInValid(): void
    {
        $o = new ParseConfig();
        $use = uniqid("testCase");
        $file = __DIR__ . "/../../resources/error/invalidNode.json";
        $schema = $o->getSchemaDir() . "/node.json";
        $this->expectException(DefineConfigException::class);
        $this->expectErrorMessage("{$use} [{$file}] json解析失败 <4> [Syntax error]");
        $this->expectErrorMessage(
            "{$use} 格式不正确\n"
            . "[] The property nv is not defined and the definition does not allow additional properties"
        );
        $o->validSchema($o->getJsonFile($file, $use), $o->getJsonFile($schema, $use), $use);
    }
}

# end of file
